#include "widget.h"
#include "ui_widget.h"
#include "mstartscene.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置窗口大小
    this->setFixedSize(1716,1019);
    this->setWindowTitle("Card_Game");
    // 创建视图
    mGameView.setSceneRect(QRect(0,0,1716,1019));//视图
    //背景音乐
    mSound1=new Sound;
    mSound1->playSound(":/new/prefix3/music/CityTears.mp3",true);
    mSound1->setVolume(0.03);
    //从场景1转到说明场景
    connect(m_StartScene.InsBtn,&QToolButton::clicked,[this](){
        mGameView.setScene(m_InstructionsScene.mInstructionsScene);
        mGameView.show();
    });
    // 从说明场景转到场景1
    connect(m_InstructionsScene.ReturnButton,&Btn::clicked,[this](){
        mGameView.setScene(m_StartScene.mStartScene1);
        mGameView.show();
    });
    //从场景1转到场景关卡
    connect(m_StartScene.startBtn,&QToolButton::clicked,[this](){
        mGameView.setScene(m_gameLevelScene.mGameLevelScene);
        mGameView.show();
    });
    //关卡返回上一页
    connect(m_gameLevelScene.LevelBtn4,&Btn::clicked,[this](){
        mGameView.setScene(m_StartScene.mStartScene1);
        mGameView.show();
    });
    //场景1关闭窗口按钮
    connect(m_StartScene.closeBtn, &QToolButton::clicked, this, &QWidget::close);
    //从关卡场景转到游戏场景
    connect(m_gameLevelScene.LevelBtn1,&Btn::clicked,[this](){
        if(n==0)
        {
            mGameView.setScene(mDeck.CardScene->mScene2);
            mGameView.show();
        }
    });
    connect(m_gameLevelScene.LevelBtn2,&Btn::clicked,[this](){
        if(n==1)
        {
            mGameView.setScene(mDeck.CardScene->mScene2);
            mGameView.show();
        }
    });
    connect(m_gameLevelScene.LevelBtn3,&Btn::clicked,[this](){
        if(n==2)
        {
            mGameView.setScene(mDeck.CardScene->mScene2);
            mGameView.show();
        }
    });
    //场景2发牌按钮
    connect(mDeck.CardScene->HandOutBtn,&Btn::clicked,&mDeck,&Deck::dealCards);
    connect(mDeck.CardScene->HandOutBtn,&Btn::clicked,[this](){
        mDeck.CardScene->mScene2->removeItem(mDeck.CardScene->HandOutBtn);
    });
    //出现出牌弃牌按钮
    connect(mDeck.CardScene->HandOutBtn,&Btn::clicked,[this](){
        mDeck.CardScene->mScene2->addItem(mDeck.CardScene->InBtn);
        mDeck.CardScene->mScene2->addItem(mDeck.CardScene->OutBtn);
    });
    //场景2出牌按钮
    connect(mDeck.CardScene->InBtn,&Btn::clicked,[this](){
        mDeck.InReady();
        qDebug()<<mDeck.ReadyCards.size();
        if(mDeck.ReadyCards.size()>1&&mDeck.ReadyCards.size()<6)
        {
            mDeck.InAlready();
            InNum+=1;
            qDebug()<<mDeck.TheScore;
            mDeck.CardScene->Changetext(mDeck.TheScore,WinScore[n],5-InNum,4-OutNum);
            if(mDeck.TheScore>=WinScore[n])//胜利条件
            {
                n+=1;
                if(n==1||n==2)
                {
                    for(int i=0;i<8;i++)
                    {
                        mDeck.CardScene->mScene2->removeItem(mDeck.HandCards[i]);
                    }
                    mDeck.HandCards.clear();
                    mDeck.TheScore=0;
                    mGameView.setScene(m_midWinScene.mMidWinScene);
                    mGameView.show();
                }
                else
                {
                    mGameView.setScene(m_winScene.mWinScene);
                    mGameView.show();
                }
            }
            else if(mDeck.TheScore<WinScore[n] && InNum==5)
            {
                mGameView.setScene(m_failScene.mFailScene);
                mGameView.show();//失败直接退出
            }
        }
        else
        {
            mDeck.Utils_1->showFloatingText(mDeck.CardScene->mScene2,"出牌数量请保持在2~5之间",QPointF(730,350),2000);
        }
    });
    //中间胜利界面到选择关卡界面
    connect(m_midWinScene.NextButton,&Btn::clicked,[this](){
        if(n==1)
        {
            m_gameLevelScene.mGameLevelScene->removeItem(m_gameLevelScene.LevelBtn4);
            m_gameLevelScene.LevelBtn2->setPixmap(QPixmap(":/new/prefix1/pic/Sec.png"));
        }
        if(n==2)
        {
            m_gameLevelScene.LevelBtn3->setPixmap(QPixmap(":/new/prefix1/pic/Thi.png"));
        }
        mGameView.setScene(m_gameLevelScene.mGameLevelScene);
        mGameView.show();
        //更改目标分数和当前分数
        mDeck.CardScene->Changetext(0,WinScore[n],5,4);
        //刷新牌的状态
        for(int i=0;i<52;i++)
        {
            mDeck.isOut[i]=true;
        }
        //重新发牌
        InNum=0;
        OutNum=0;
        mDeck.dealCards();
    });
    //场景2弃牌按钮
    connect(mDeck.CardScene->OutBtn,&Btn::clicked,[this](){
        OutNum+=1;
        if(OutNum==5){
            mDeck.CardScene->Changetext(mDeck.TheScore,WinScore[n],5-InNum,0);
        }
        else{
        mDeck.CardScene->Changetext(mDeck.TheScore,WinScore[n],5-InNum,4-OutNum);
        }
        if(OutNum==5)
        {
            mDeck.Utils_1->showFloatingText(mDeck.CardScene->mScene2,"很遗憾 您的弃牌次数已用完",QPointF(730,350),2000);
            OutNum-=1;
        }
        else
        {
            mDeck.OutAlready();
        }
    });
    //场景3结束游戏按钮
    connect(m_failScene.EndButton,&Btn::clicked,this, &QWidget::close);
    connect(m_winScene.EndButton,&Btn::clicked,this, &QWidget::close);
    //设置视图场景1
    mGameView.setScene(m_StartScene.mStartScene1);
    //设置视图父亲
    mGameView.setParent(this);
    mGameView.show();
}

Widget::~Widget()
{
    delete ui;
}
